package Swing;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JPrincipal.java
 *
 * Created on 23/Mai/2011, 23:01:35
 */


import code.IO;
import java.util.TreeMap;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import code.User;

/**
 *
 * @author Tiago
 */
public class JPrincipal extends javax.swing.JFrame {

    public TreeMap<String, User> rede;
    private DefaultListModel listaUtilizadores;
    private DefaultListModel listaAmigos;

    /** Creates new form JPrincipal */
    public JPrincipal(TreeMap<String, User> users) {
        initComponents();

        jRadioButtonNome.setSelected(true);

        // para as Jlist com informacao
        listaUtilizadores = new DefaultListModel();
        listaAmigos = new DefaultListModel();

        // estrutura da rede
        rede = users;
        jListUtilizadores.setModel(listaUtilizadores);
        jListAmigos.setModel(listaAmigos);

        carregaListaUtilizadores("nome");
    }

    private void carregaListaUtilizadores(String tipo) {
        if (tipo.equalsIgnoreCase("nome")) {
            for (User u : rede.values()) {
                listaUtilizadores.addElement(u.getNome());
            }
        }
        if (tipo.equalsIgnoreCase("nick")) {
            for (User u : rede.values()) {
                listaUtilizadores.addElement(u.getNick());
            }
        }
    }

  
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel3 = new javax.swing.JPanel();
        jPanel6 = new javax.swing.JPanel();
        jRadioButtonNome = new javax.swing.JRadioButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jListUtilizadores = new javax.swing.JList();
        jButtonRemover = new javax.swing.JButton();
        jRadioButtonNick = new javax.swing.JRadioButton();
        jLabel2 = new javax.swing.JLabel();
        jButtonInserir = new javax.swing.JButton();
        jTextFieldProcurar = new javax.swing.JTextField();
        jButtonProcurar = new javax.swing.JButton();
        jPanel7 = new javax.swing.JPanel();
        jScrollPane6 = new javax.swing.JScrollPane();
        jTextPaneMorada = new javax.swing.JTextPane();
        jLabelMorada = new javax.swing.JLabel();
        jScrollPaneNick = new javax.swing.JScrollPane();
        jTextPaneNick = new javax.swing.JTextPane();
        jLabelRelacoes = new javax.swing.JLabel();
        jLabelNick = new javax.swing.JLabel();
        jLabelNome = new javax.swing.JLabel();
        jScrollPane9 = new javax.swing.JScrollPane();
        jTextPaneNome = new javax.swing.JTextPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        jListAmigos = new javax.swing.JList();
        jLabelMail = new javax.swing.JLabel();
        jScrollPaneMail = new javax.swing.JScrollPane();
        jTextPaneMail = new javax.swing.JTextPane();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItemImportarUtilizadores = new javax.swing.JMenuItem();
        jMenuItemImportarRelacoes = new javax.swing.JMenuItem();

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel3.setMaximumSize(new java.awt.Dimension(35767, 35767));
        jPanel3.setPreferredSize(new java.awt.Dimension(698, 600));

        jPanel6.setBorder(javax.swing.BorderFactory.createTitledBorder("Procurar"));

        jRadioButtonNome.setText("Nome");
        jRadioButtonNome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonNomeActionPerformed(evt);
            }
        });

        jListUtilizadores.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jListUtilizadoresValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(jListUtilizadores);

        jButtonRemover.setText("Remover");

        jRadioButtonNick.setText("Nickname");
        jRadioButtonNick.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonNickActionPerformed(evt);
            }
        });

        jLabel2.setText("Utilizador");

        jButtonInserir.setText("Inserir");
        jButtonInserir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonInserirActionPerformed(evt);
            }
        });

        jTextFieldProcurar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldProcurarActionPerformed(evt);
            }
        });

        jButtonProcurar.setText("Procurar");
        jButtonProcurar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonProcurarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 142, Short.MAX_VALUE)
                    .addComponent(jTextFieldProcurar, javax.swing.GroupLayout.DEFAULT_SIZE, 142, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jRadioButtonNome)
                    .addComponent(jRadioButtonNick)
                    .addComponent(jLabel2)
                    .addComponent(jButtonInserir)
                    .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jButtonProcurar)
                        .addComponent(jButtonRemover)))
                .addGap(59, 59, 59))
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jRadioButtonNome)
                        .addGap(19, 19, 19)
                        .addComponent(jRadioButtonNick)
                        .addGap(31, 31, 31)
                        .addComponent(jLabel2)
                        .addGap(8, 8, 8)
                        .addComponent(jButtonInserir)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButtonRemover)
                        .addGap(232, 232, 232))
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 284, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextFieldProcurar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButtonProcurar))
                        .addGap(3, 3, 3)))
                .addContainerGap())
        );

        jPanel7.setBorder(javax.swing.BorderFactory.createTitledBorder("Informação"));

        jTextPaneMorada.setEditable(false);
        jScrollPane6.setViewportView(jTextPaneMorada);

        jLabelMorada.setText("Morada:");

        jTextPaneNick.setEditable(false);
        jScrollPaneNick.setViewportView(jTextPaneNick);

        jLabelRelacoes.setText("Amizades:");

        jLabelNick.setText("Nickname:");

        jLabelNome.setText("Nome:");

        jTextPaneNome.setEditable(false);
        jScrollPane9.setViewportView(jTextPaneNome);

        jScrollPane2.setViewportView(jListAmigos);

        jLabelMail.setText("Mail:");

        jTextPaneMail.setEditable(false);
        jScrollPaneMail.setViewportView(jTextPaneMail);

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabelNome)
                    .addComponent(jLabelNick)
                    .addComponent(jLabelRelacoes)
                    .addComponent(jLabelMorada)
                    .addComponent(jLabelMail))
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel7Layout.createSequentialGroup()
                        .addGap(4, 4, 4)
                        .addComponent(jScrollPane9, javax.swing.GroupLayout.DEFAULT_SIZE, 268, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel7Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPaneNick, javax.swing.GroupLayout.DEFAULT_SIZE, 268, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel7Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane6, javax.swing.GroupLayout.DEFAULT_SIZE, 268, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel7Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPaneMail, javax.swing.GroupLayout.DEFAULT_SIZE, 268, Short.MAX_VALUE))
                    .addGroup(jPanel7Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, 0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabelNome)
                    .addComponent(jScrollPane9, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabelNick)
                    .addComponent(jScrollPaneNick, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabelMorada)
                    .addComponent(jScrollPane6, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(13, 13, 13)
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPaneMail, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelMail))
                .addGap(18, 18, 18)
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabelRelacoes)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, 352, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(jPanel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("Utilizador", jPanel3);

        jPanel2.add(jTabbedPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 690, 380));

        getContentPane().add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 690, 380));

        jMenu1.setText("Ficheiro");

        jMenuItemImportarUtilizadores.setText("Importar Utilizadores");
        jMenuItemImportarUtilizadores.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemImportarUtilizadoresActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItemImportarUtilizadores);

        jMenuItemImportarRelacoes.setText("Importar Relações");
        jMenuItemImportarRelacoes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemImportarRelacoesActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItemImportarRelacoes);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonInserirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonInserirActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButtonInserirActionPerformed

    private void jListUtilizadoresValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jListUtilizadoresValueChanged
        if (jRadioButtonNome.isSelected()) {
            String nome = jListUtilizadores.getSelectedValue().toString();

            for (User u : rede.values()) {
                if (u.getNome().equalsIgnoreCase(nome)) {
                    jTextPaneNome.setText(u.getNome());
                    jTextPaneNick.setText(u.getNick());
                    jTextPaneMorada.setText(u.getMorada());
                    jTextPaneMail.setText(u.getMail());

                    listaAmigos = new DefaultListModel();

                    TreeMap<String, User> lista_amigos = u.getAmigos();

                    for (User amigo : lista_amigos.values()) {
                        listaAmigos.addElement(amigo.getNome());
                    }
                }

            }
        }
        if (jRadioButtonNick.isSelected()) {
            String nick = jListUtilizadores.getSelectedValue().toString();

            for (User u : rede.values()) {
                if (u.getNick().equalsIgnoreCase(nick)) {
                    jTextPaneNick.setText(u.getNick());
                    jTextPaneNome.setText(u.getNome());
                    jTextPaneMorada.setText(u.getMorada());
                    jTextPaneMail.setText(u.getMail());

                    listaAmigos = new DefaultListModel();

                    TreeMap<String, User> lista_amigos = u.getAmigos();

                    for (User amigo : lista_amigos.values()) {
                        listaAmigos.addElement(amigo.getNome());
                    }
                }

            }
        }
    }//GEN-LAST:event_jListUtilizadoresValueChanged

    private void jRadioButtonNomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonNomeActionPerformed
        jRadioButtonNome.setSelected(true);
        jRadioButtonNick.setSelected(false);
        listaUtilizadores = new DefaultListModel();
        carregaListaUtilizadores("nome");
        jListUtilizadores.setModel(listaUtilizadores);
}//GEN-LAST:event_jRadioButtonNomeActionPerformed

    private void jRadioButtonNickActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonNickActionPerformed
        jRadioButtonNome.setSelected(false);
        jRadioButtonNick.setSelected(true);
        listaUtilizadores = new DefaultListModel();
        carregaListaUtilizadores("nick");
        jListUtilizadores.setModel(listaUtilizadores);
}//GEN-LAST:event_jRadioButtonNickActionPerformed

    private void jTextFieldProcurarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldProcurarActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextFieldProcurarActionPerformed

    private void jButtonProcurarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonProcurarActionPerformed
        String texto_procura = jTextFieldProcurar.getText();
        
        if (texto_procura.equalsIgnoreCase("") == false || texto_procura.equalsIgnoreCase(null) == false) {
            
            this.jRadioButtonNomeActionPerformed(evt);
            
            listaUtilizadores = new DefaultListModel();

            for (User u : rede.values()) {
                if (u.getNome().contains(texto_procura) || u.getMorada().contains(texto_procura) || u.getMail().contains(texto_procura) || u.getNick().contains(texto_procura)) {
                    listaUtilizadores.addElement(u.getNome());
                }

            }
            jListUtilizadores.setModel(listaUtilizadores);
        } else {
            JOptionPane.showMessageDialog(rootPane, "Erro", texto_procura, 0);
        }
    }//GEN-LAST:event_jButtonProcurarActionPerformed

    
    private void jMenuItemImportarUtilizadoresActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemImportarUtilizadoresActionPerformed
        code.IO.importaBDusers();
      
    }//GEN-LAST:event_jMenuItemImportarUtilizadoresActionPerformed

    private void jMenuItemImportarRelacoesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemImportarRelacoesActionPerformed

        
        
        
    }//GEN-LAST:event_jMenuItemImportarRelacoesActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new JPrincipal(null).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonInserir;
    private javax.swing.JButton jButtonProcurar;
    private javax.swing.JButton jButtonRemover;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabelMail;
    private javax.swing.JLabel jLabelMorada;
    private javax.swing.JLabel jLabelNick;
    private javax.swing.JLabel jLabelNome;
    private javax.swing.JLabel jLabelRelacoes;
    private javax.swing.JList jListAmigos;
    private javax.swing.JList jListUtilizadores;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItemImportarRelacoes;
    private javax.swing.JMenuItem jMenuItemImportarUtilizadores;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JRadioButton jRadioButtonNick;
    private javax.swing.JRadioButton jRadioButtonNome;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane6;
    private javax.swing.JScrollPane jScrollPane9;
    private javax.swing.JScrollPane jScrollPaneMail;
    private javax.swing.JScrollPane jScrollPaneNick;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTextField jTextFieldProcurar;
    private javax.swing.JTextPane jTextPaneMail;
    private javax.swing.JTextPane jTextPaneMorada;
    private javax.swing.JTextPane jTextPaneNick;
    private javax.swing.JTextPane jTextPaneNome;
    // End of variables declaration//GEN-END:variables
}
